'use strict';

let express = require('express'),
  cookieParser = require('cookie-parser'),
  root_controller = require('./root_controller'),
  auth_controller = require('./auth_controller'),
  common_controller = require('./common_controller'),
  postController = require('./post_controller'),
  pollController = require('./poll_controller'),
  reportController = require('./report_controller'),
  commentController = require('./comment_controller'),
  vehicleController = require('./vehicle_controller'),
  dataController = require('./data_controller'),
  spiderController = require('./spider_controller'),
  userController = require('./user_controller'),
  middlewares = require('./middlewares'),
  multer = require('multer'),
  cst = require('../../conf/const');

let router_api = express.Router(),
  router_common = express.Router(),
  router_auth = express.Router(),
  router_root = express.Router(),
  postRouter = express.Router(),
  pollRouter = express.Router(),
  reportRouter = express.Router(),
  vehicleRouter = express.Router(),
  spiderRouter = express.Router(),
  dataRouter = express.Router(),
  commentRouter = express.Router(),
  userRouter = express.Router();

let storage = multer.memoryStorage(),
  upload = multer({storage: storage});

//router_api.use(middlewares.authenticate);
router_api.use(cookieParser(cst.SESSION_SECRET));
router_root.use(cookieParser(cst.SESSION_SECRET));
router_api.use(middlewares.responseTime());
router_root.use(middlewares.responseTime());

router_auth.use(/^(?!\/login)/, auth_controller.checkLogin); // 检查是否已登录
// curl -XGET 'http://kajiaapp.com/nnc/auth/showSession'
router_auth.get('/showSession', auth_controller.showSession);
// curl -XPOST 'http://kajiaapp.com/nnc/auth/login' -d 'name=zhaoyong&passwd=123456'
router_auth.post('/login', auth_controller.login);
// http://kajiaapp.com/nnc/auth/logout
router_auth.post('/logout', auth_controller.logout);

router_common.post('/upload', upload.any(), common_controller.upload);

//postRouter.use(auth_controller.checkLogin);// 检查是否已登录
// curl -XGET 'http://kajiaapp.com/nnc/post'
// curl -XPOST 'http://kajiaapp.com/nnc/post' -d 'title=场馆名称_2&content=城市_2&author=2'
postRouter
  .route('/+')
  .get(postController.getPostByPage)
  .post(postController.addPost);

postRouter.route('/+user').post(postController.addPostForUser);

postRouter.route('/+getPostCategory').get(postController.getPostCategory);

// curl -XDELETE 'http://kajiaapp.com/nnc/post/2'
// curl -XPATCH 'http://kajiaapp.com/nnc/post/2' -d 'title=更新后的&orders=10&dataTypeId=1&url=http://www.baidu.com&packId=1'
postRouter
  .route('/:postId')
  .get(postController.getPostById)
  .delete(postController.deletePostByIds)
  .put(postController.updatePost);

// curl -XGET 'http://kajiaapp.com/nnc/comment'
commentRouter.route('/+').get(commentController.getCommentByPostId);

commentRouter.route('/:commentId').delete(commentController.deleteCommentByIds);

//vehicleRouter.use(auth_controller.checkLogin);// 检查是否已登录
// curl -XGET 'http://kajiaapp.com/nnc/vehicle/getVehicleBrandByCapital?capital=A'
// curl -XGET 'http://kajiaapp.com/nnc/vehicle/getVehicleSpecByModelId?modelId=A' #获取model下的spec列表
//curl -XPUT 'http://kajiaapp.com/nnc/vehicle/spec/1' -d 'marketTim=2017'
vehicleRouter
  .route('/+getVehicleBrandByCapital')
  .get(vehicleController.getVehicleBrandByCapital);

vehicleRouter
  .route('/+getVehicleSpecByModelId')
  .get(vehicleController.getVehicleSpecByModelId);

vehicleRouter
  .route('/+spec/:specId')
  .get(vehicleController.getVehicleSpecBySpecId)
  .put(vehicleController.updateSpecData);

//curl -XGET 'http://kajiaapp.com/nnc/vehicle/getModelDetailBySpecId'
vehicleRouter
  .route('/+getModelDetailBySpecId')
  .get(vehicleController.getModelDetailBySpecId);

//curl -XPOST 'http://kajiaapp.com/nnc/vehicle/updateModelDetail/1' -d 'typeId=2017'
vehicleRouter
  .route('/+updateModelDetail/:modelId')
  .post(vehicleController.updateModelDetailByModelId);

vehicleRouter.route('/+model').post(vehicleController.addModel);

//curl -XGET 'http://kajiaapp.com/nnc/vehicle/getCarType'
vehicleRouter.route('/+getCarType').get(vehicleController.getCarType);

vehicleRouter
  .route('/+getBrandListByNamePrefix')
  .get(vehicleController.getBrandListByNamePrefix);

//dataRouter.use(auth_controller.checkLogin);// 检查是否已登录
// curl -XPOST 'http://kajiaapp.com/nnc/operate' -d 'title=标题&subtitle=副标题&img="https://f11.baidu.com/it/u=3921373078,4265237256&fm=72"&validateDate=2020-10-01&releateArticle=123'
dataRouter
  .route('/+')
  .get(dataController.getOperateByPage)
  .post(dataController.addData);

// curl -XDELETE 'http://kajiaapp.com/nnc/operate/2'
// curl -XPATCH 'http://kajiaapp.com/nnc/operate/2' -d 'title=更新后的&orders=10&dataTypeId=1&url=http://www.baidu.com&packId=1'
dataRouter
  .route('/:operateId')
  .delete(dataController.deleteOperateByIds)
  .put(dataController.updateData);

dataRouter
  .route('/+moveOperatePosition')
  .post(dataController.moveOperatePosition);

//userRouter.use(auth_controller.checkLogin);// 检查是否已登录
// curl -XGET 'http://kajiaapp.com/nnc/user/getByAccountName'
userRouter.route('/+getByAccountName').get(userController.getByAccountName);

// curl -XGET 'http://kajiaapp.com/nnc/user/getUserInfo'
userRouter.route('/+getUserInfo').get(userController.getUserInfo);

// curl -XPOST 'http://kajiaapp.com/nnc/user/blockUserByIds' -d 'userIds=2'
userRouter.route('/+blockUserByIds').post(userController.blockUserByIds);

// curl -XPOST 'http://kajiaapp.com/nnc/user/generateMobileMsgCode' -d 'mobile=15652361404'
userRouter
  .route('/+generateMobileMsgCode')
  .post(userController.generateMobileMsgCode);

// curl -XPOST 'http://kajiaapp.com/nnc/user/generateMobileMsgCode' -d 'mobile=15652361404'
userRouter
  .route('/+getUserListByNamePrefix')
  .get(userController.getUserListByNamePrefix);

// curl -XGET 'http://kajiaapp.com/nnc/user/getUserListByPage'
userRouter.route('/+getUserListByPage').get(userController.getUserByPage);

// curl -XPOST 'http://kajiaapp.com/nnc/user/modifyUserRole'
userRouter.route('/+modifyUserRole').post(userController.modifyUserRole);

//router_root.use(middlewares.authenticate);
router_root.get('/', middlewares.authenticate, root_controller.index);
router_root.get('/login', root_controller.login);

//spiderRouter.use(auth_controller.checkLogin);// 检查是否已登录
// curl -XPOST 'http://kajiaapp.com/nnc/spider/brand' -d 'capital=A&autohomeId=123&brandName=123&logoUrl=123'
spiderRouter.route('/+brand').post(spiderController.addBrand);
spiderRouter.route('/+model').post(spiderController.addModel);

spiderRouter
  .route('/+getBrandIdFromAutoHomeId')
  .get(spiderController.getBrandIdFromAutoHomeId);

//reportRouter.use(auth_controller.checkLogin);// 检查是否已登录
// curl -XGET 'http://kajiaapp.com/nnc/report'
reportRouter.route('/+').get(reportController.getReportByPage);

// curl -XDELETE 'http://kajiaapp.com/nnc/report/2'
reportRouter.route('/:reportId').delete(reportController.deleteReportByIds);

pollRouter.route('/+add').post(pollController.add);

exports.api = router_api;
exports.commonRouter = router_common;
exports.postRouter = postRouter;
exports.pollRouter = pollRouter;
exports.reportRouter = reportRouter;
exports.vehicleRouter = vehicleRouter;
exports.spiderRouter = spiderRouter;
exports.dataRouter = dataRouter;
exports.userRouter = userRouter;
exports.authRouter = router_auth;
exports.commentRouter = commentRouter;
exports.root = router_root;
